package com.sdkx.wear_os.customview

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.sdkx.wear_os.R
import com.sdkx.wear_os.app.MyApplication

/**
 * 弹窗可使用第三方 gradle直接集成 'com.dou361.dialogui:jjdxm-dialogui:1.0.3'
 *
 */
/**
 * 有圓角的自定义dialog
 * @param context     上下文
 * @param widthValue  宽度比例
 * @param heightValue 高度比例
 * @param layout        视图布局
 * @param style       动画样式
 * @param gravity     弹窗显示位置
 * @return
 */
class CustomizeDialog(
    context: Context?,
    widthValue: Int,
    heightValue: Int,
    layout: View?,
    style: Int,
    gravity: Int
) : Dialog(context!!, style) {
    init {
        if (layout != null) {
            setContentView(layout)
        }
        val params: WindowManager.LayoutParams = window!!.attributes
        params.width = widthValue
        params.height = heightValue
        params.gravity = gravity
        window!!.attributes = params
    }

    override fun dismiss() {
        val view = currentFocus
        if (view is TextView) {
            val mInputMethodManager =
                context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            mInputMethodManager.hideSoftInputFromWindow(
                view.getWindowToken(),
                InputMethodManager.RESULT_UNCHANGED_SHOWN
            )
        }
        super.dismiss()
    }
}

/**
 * 弹窗样式(中间)
 * @param context 上下文
 * @param view 布局
 * @param isCancel 点外界是否可取消
 * @param widthValue 宽度
 * @param heightValue 高度
 * @param x 偏移量
 * @param y 偏移量
 */
fun customizeDialogCenter(
    context: Context,
    view: View,
    isCancel: Boolean,
    widthValue: Int = MyApplication.getWidth() / 4 * 3,
    heightValue: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
    x: Int = 0, y: Int = 0
): Dialog {
    val dialog = Dialog(context, R.style.dialog)
    dialog.setContentView(view)
    dialog.setCancelable(isCancel)
    val params: WindowManager.LayoutParams = dialog.window!!.attributes
    params.width = widthValue
    params.height = heightValue
    params.gravity = Gravity.CENTER
    params.x = x
    params.y = y
    dialog.window!!.attributes = params
    return dialog
}

/**
 * 弹窗样式(底部)
 * @param context 上下文
 * @param view 布局
 * @param isCancel 点外界是否可取消
 * @param widthValue 宽度
 * @param heightValue 高度
 * @param x 偏移量
 * @param y 偏移量
 */
fun customizeDialogBottom(
    context: Context,
    view: View,
    isCancel: Boolean,
    widthValue: Int = MyApplication.getWidth(),
    heightValue: Int = ViewGroup.LayoutParams.WRAP_CONTENT
): Dialog {
    val dialog = Dialog(context, R.style.dialog)
    dialog.setContentView(view)
    dialog.setCancelable(isCancel)
    val window = dialog.window
    window?.setGravity(Gravity.BOTTOM)
    window?.setWindowAnimations(R.style.dialogButtonInStyle)//需要去除R.style.dialog中windowIsFloating属性才生效
    window?.setLayout(widthValue, heightValue)
    return dialog
}

/**
 * 弹窗样式(底部)
 * @param context 上下文
 * @param view 布局
 * @param isCancel 点外界是否可取消
 * @param heightValue 高度
 */
fun customizeDialogBottom(
    context: Context,
    view: View,
    isCancel: Boolean,
    heightValue: Int
): BottomSheetDialog {
    val dialogBottom = BottomSheetDialog(context)
    dialogBottom.setContentView(view)
    dialogBottom.setCancelable(isCancel)
    val params: WindowManager.LayoutParams = dialogBottom.window!!.attributes
    params.height = heightValue
    dialogBottom.window!!.attributes = params
    return dialogBottom
}

